package cn.wolfcode.crm.web.controller;

import cn.wolfcode.crm.domain.Customer;
import cn.wolfcode.crm.query.CustomerQueryObject;
import cn.wolfcode.crm.service.ICustomerService;
import cn.wolfcode.crm.service.IEmployeeService;
import cn.wolfcode.crm.util.JSONResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
@RequestMapping("/customerPool")
public class CustomerPoolController {
	
	@Autowired
	private ICustomerService customerService;

	@Autowired
	private IEmployeeService employeeService;

	@RequestMapping("list")
	public String list(Model model, @ModelAttribute("qo")CustomerQueryObject qo){
		qo.setStatus(Customer.STATUS_POOL);
		model.addAttribute("result", customerService.query(qo));
		model.addAttribute("sellers",employeeService.selectAll());
		System.out.println(employeeService.selectAll());
		return "/customer/customer_pool";
	}

	@RequestMapping("/saveOrUpdate")
	@ResponseBody
	public Object saveOrUpdate(Customer entity){
		JSONResult result = new JSONResult();
		try {
			//int i = 1/0;
			if(entity.getId() != null){
				customerService.updateByPrimaryKey(entity);
			}else{
				customerService.insert(entity);
			}
		} catch (Exception e) {
			e.printStackTrace();
			result.mark(e.getMessage());
		}

		return result;
	}
}
